******************************************
clear 
use "${output_stata}\reg_perf_corrig.dta"

tab entree

*Exports survival
gen survival_5 = s001_5 > 0
gen survival_6 = s001_6 > 0
gen survival_7 =  s001_7 > 0

foreach x of varlist s001_4 {
	su `x',d 
	replace `x' = `r(p95)' if `x' > `r(p95)'
	replace `x' = `r(p5)' if `x' < `r(p5)'
}

drop if s001_4== . | s001_4 == 0

gen g_sales_4_7 = (s001_7 - s001_4)/s001_4
gen g_sales_4_5 = (s001_5 - s001_4)/s001_4
gen log_sales_4_7 = log(s001_7/s001_4)
gen log_sales_4 = log(s001_4)
gen log_sales_7 = log(s001_7)

gen g_eff_4_7  = (eff_7 - eff_4)/eff_4
gen g_eff_3_7  = (eff_7 - eff_3)/eff_3
gen g_eff_3_4  = (eff_4 - eff_3)/eff_3
gen g_eff_4_5  = (eff_5 - eff_4)/eff_4
gen d_eff_3_4  = 2*(eff_4 - eff_3)/(eff_3+eff_4)
gen d_eff_3_7  = 2*(eff_7 - eff_3)/(eff_3 + eff_7)

gen g_HC_distance_3_7  = (HC_dist_var_7 - HC_dist_var_3)/HC_dist_var_3
gen g_HC_distance_4_7  = (HC_dist_var_7 - HC_dist_var_4)/HC_dist_var_4
gen g_HC_distance_3_5  = (HC_dist_var_5 - HC_dist_var_3)/HC_dist_var_3
gen g_HC_distance_4_5  = (HC_dist_var_5 - HC_dist_var_4)/HC_dist_var_4

gen d_top5_3_7  = 2*(top5_7 - top5_3)/(eff_3 + eff_7)
gen d_nottop5_3_7  = 2*((eff_7 - top5_7) - (eff_3-top5_3))/(eff_7  + eff_3)

gen g_top5_3_7  = (top5_7 - top5_3)/(eff_3)
gen g_nottop5_3_7  = ((eff_7 - top5_7) - (eff_3-top5_3))/(eff_3)

gen g_top5_3_7b  = (top5_7 - top5_3)/(top5_3)
gen g_nottop5_3_7b  = (not_top5_7 - not_top5_3)/(not_top5_3)

gen d_top5_3_6  = 2*(top5_6 - top5_3)/(eff_3 + eff_6)
gen d_nottop5_3_6  = 2*((eff_6 - top5_6) - (eff_3-top5_3))/(eff_6  + eff_3)
gen d_eff_3_6  = 2*(eff_6 - eff_3)/(eff_3 + eff_6)

set more off
foreach v of varlist g_* d_* {
su `v',d
gen p50 = r(p50)
gen p95 = r(p95)
gen p5 = r(p5)
replace `v' =. if `v'<p50-2*(p95-p5)|`v'>p50+2*(p95-p5)
drop p50 p95 p5
}

replace intra_groupe = 0 if intra_groupe == .
replace intra_groupe_exit= 0 if intra_groupe_exit == .
gen g_eff_ig = (intra_groupe-intra_groupe_exit)/eff_3
gen g_eff_not_ig = (eff_7 - eff_3 - (intra_groupe-intra_groupe_exit))/eff_3

gen buy = entree == "external"
gen HC_buy = HC_distance_t1*buy 
gen q_HC2_buy = q_HC2*buy 

label var HC_distance_t1 "HC Distance$\_{\textit{g,n,t-1}}$"
label var HC_distance_t1 "HC Distance$\_{\textit{g,n,t-1}}$"
label var q_HC2 "1(High HC Distance)$\_{\textit{g,n,t-1}}$"
label var buy "1(Buy)$\_{\textit{g,n,t}}$"
label var q_HC2_buy "1(High HC Distance)$\_{\textit{g,n,t-1}}$ $\times$ 1(Buy)$\_{\textit{g,n,t}}$"
label var HC_buy "HC Distance$\_{\textit{g,n,t-1}}$ $\times$ 1(Buy)$\_{\textit{g,n,t}}$"

egen orig = group(apgr_1)	
egen dest = group(code_entry)	


reghdfe g_HC_distance_3_7 g_eff_3_7  g_top5_3_7 g_nottop5_3_7 HC_distance_t1  vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if  delta_b==3 & entree == "int_const" | entree == "external", absorb(orig_dest_year) cluster(code_entry_num apgr_1)
gen sample = e(sample)

// quartiles size
preserve
keep if year == year_entry -1 
gquantiles q_size_t1 = size_t1, nq(4) xtile
keep sirtg siren year_entry code_entry q_size_t1
save "${input_stata}\size_dummy.dta", replace
restore

merge m:1 sirtg siren year_entry code_entry using "${input_stata}\size_dummy.dta"
erase "${input_stata}\size_dummy.dta"
drop _m


*************
reghdfe g_HC_distance_4_7 g_eff_3_7  g_top5_3_7 g_nottop5_3_7 HC_distance_t1  vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if  delta_b==3 & (entree == "external" | entree == "int_const"), absorb(orig_dest_year) cluster(code_entry_num apgr_1)
gen sample2 = e(sample)

label var g_eff_ig "$\Delta$ Internal flows$\_{\textit{g,t-1,t+3}}$"
label var g_eff_not_ig "$\Delta$ External flows$\_{\textit{g,t-1,t+3}}$"
label var g_sales_4_7 "Growth$\_{\textit{g,n,t-1,t+3}}$"

eststo clear
eststo summa: quietly estpost summ ///
g_eff_ig g_eff_not_ig g_sales_4_7 ///
if sample2 & delta_b==3, d

esttab summa using "${export}\stat_summ_flows.tex", ///
cells("count(fmt(%15.0fc)) mean(fmt(2)) sd(fmt(2)) p5(fmt(2)) p25(fmt(2)) p50(fmt(2)) p75(fmt(2)) p95(fmt(2))") ///
star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)  /* 
*/ nomtitle noobs nonumber fragment booktabs label mlabels(none) collabels(none) noline replace


*************
* Table post entry labor 
*************
eststo clear

*BUILD
reghdfe g_HC_distance_3_7 HC_distance_t1  vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy ==0, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)	
estimates store bui_dyn1
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_eff_3_7 HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy ==0, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)	
estimates store bui_dyn2
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_top5_3_7 HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if  delta_b==3 & sample == 1 & buy ==0, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)	
estimates store bui_dyn3
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_nottop5_3_7 HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if  delta_b==3 & sample == 1 & buy ==0, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)		
estimates store bui_dyn4
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_eff_ig HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy ==0, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)	
estimates store bui_dyn5
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_eff_not_ig HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy ==0, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store bui_dyn6
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

*BUILD
reghdfe g_HC_distance_3_7 HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)	
estimates store buy_dyn1
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_eff_3_7 HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)	
estimates store buy_dyn2
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_top5_3_7 HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if  delta_b==3 & sample == 1 & buy == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)	
estimates store buy_dyn3
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_nottop5_3_7 HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if  delta_b==3 & sample == 1 & buy == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)		
estimates store buy_dyn4
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_eff_ig HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)	
estimates store buy_dyn5
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_eff_not_ig HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store buy_dyn6
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

*INTERACTION
reghdfe g_HC_distance_3_7 HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==3 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)	
estimates store interac_dyn1
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_eff_3_7 HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==3 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)	
estimates store interac_dyn2
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_top5_3_7 HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==3 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)	
estimates store interac_dyn3
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_nottop5_3_7 HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==3 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)		
estimates store interac_dyn4
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_eff_ig HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==3 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)	
estimates store interac_dyn5
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_eff_not_ig HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==3 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)		
estimates store interac_dyn6
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"


esttab bui_dyn2 bui_dyn3 bui_dyn4 bui_dyn1 using "${export}\bui_lab_dyn.tex", /*
*/ keep(HC_distance_t1_s_brut) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N, fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))

esttab buy_dyn2 buy_dyn3 buy_dyn4 buy_dyn1 using "${export}\buy_lab_dyn.tex", /*
*/ keep(HC_distance_t1_s_brut) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N, fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))

esttab bui_dyn2 bui_dyn5 bui_dyn6 using "${export}\ig_lab_bui.tex", /*
*/ keep(HC_distance_t1_s_brut) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N, fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))

esttab buy_dyn2 buy_dyn5 buy_dyn6 using "${export}\ig_lab_buy.tex", /*
*/ keep(HC_distance_t1_s_brut) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N, fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))

esttab interac_dyn2 interac_dyn3 interac_dyn4 interac_dyn1 using "${export}\interac_lab_dyn.tex", /*
*/ keep(HC_buy) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N, fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))

esttab interac_dyn2 interac_dyn5 interac_dyn6 using "${export}\interac_ig_lab_dyn.tex", /*
*/ keep(HC_buy) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N, fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))


******************************************
* Performance in the Sector of Entry - Build or Buy
******************************************

*BUILD
reghdfe log_sales_4 HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy == 0, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store bui_sales1
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_sales_4_7 HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy == 0, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)	
estimates store bui_sales2
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe survival_7 HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy == 0, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store bui_sales3
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

*BUY
reghdfe log_sales_4 HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store buy_sales1
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_sales_4_7 HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)	
estimates store buy_sales2
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe survival_7 HC_distance_t1 vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==3 & sample == 1 & buy == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store buy_sales3
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

*INTERACTION
reghdfe log_sales_4 HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==3 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)	
estimates store interac_sales1
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe g_sales_4_7 HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==3 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)	
estimates store interac_sales2
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe survival_7 HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==3 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)	
estimates store interac_sales3
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

esttab bui_sales1 bui_sales2 bui_sales3 using "${export}\bui_sales_dyn.tex", /*
*/ keep(HC_distance_t1_s_brut)  cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace  fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N, fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))

esttab buy_sales1 buy_sales2 buy_sales3 using "${export}\buy_sales_dyn.tex", /*
*/ keep(HC_distance_t1_s_brut)  cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace  fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N, fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))

esttab interac_sales1 interac_sales2 interac_sales3 using "${export}\interac_sales_dyn.tex", /*
*/ keep(HC_buy)  cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace  fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N, fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))

******************************************
* Performance of firms that enter - alternative measures
******************************************

eststo clear

xtset id delta_b
foreach x of varlist oi_sales roa_siren ebit_eff_siren vaj_eff_siren {
	
	gen D`x' = (`x' - L3.`x')/L3.`x' if L3.`x' >0 
	su D`x',d 
	gen D`x'_w = D`x' if D`x' >= `r(p1)' & D`x' <= `r(p99)'
	replace D`x'_w = `r(p99)' if `x' > `r(p99)'
	replace D`x'_w = `r(p1)' if `x' < `r(p1)'
	su D`x'_w,d
	replace D`x'_w = (D`x'_w - `r(mean)')/ `r(sd)'
}

capture drop sample
reghdfe Doi_sales_w Droa_siren_w Debit_eff_siren_w Debit_eff_siren_w HC_distance_t1  vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if  delta_b==7, /*
*/absorb(orig_dest_year) cluster(code_entry_num apgr_1)
gen sample = e(sample)

label var Doi_sales_w "$\Delta$ Operating income/Employment$\_{\textit{g,t,t+3}}$"
label var Droa_siren_w "$\Delta$ ROA$\_{\textit{g,t,t+3}}$"

eststo clear
eststo summa: quietly estpost summ ///
Doi_sales_w Droa_siren_w ///
if sample & delta_b==7 & (buy == 1 | buy == 0), d

esttab summa using "${export}\stat_other_perf.tex", ///
cells("count(fmt(%15.0fc)) mean(fmt(2)) sd(fmt(2)) p5(fmt(2)) p25(fmt(2)) p50(fmt(2)) p75(fmt(2)) p95(fmt(2))") ///
star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)  /* 
*/ nomtitle noobs nonumber fragment booktabs label mlabels(none) collabels(none) noline replace

reghdfe Doi_sales_w HC_distance_t1_s_brut diversity_t1 size_t1 vaj_eff_t1  /*
*/  immo_eff_t1 sal_eff_t1 tresact_eff_t1 if  delta_b==7&buy==0& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store perf_1
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe Droa_siren_w HC_distance_t1_s_brut diversity_t1 size_t1 vaj_eff_t1  /*
*/  immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==7&buy==0& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store perf_2
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe Doi_sales_w HC_distance_t1_s_brut diversity_t1 size_t1 vaj_eff_t1  /*
*/  immo_eff_t1 sal_eff_t1 tresact_eff_t1 if  delta_b==7&buy==1& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store perf_3
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe Droa_siren_w HC_distance_t1_s_brut diversity_t1 size_t1 vaj_eff_t1  /*
*/  immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==7&buy==1& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store perf_4
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe Doi_sales_w HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==7 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)	
estimates store perf_5
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe Droa_siren_w HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==7 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)	
estimates store perf_6
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

esttab perf_1 perf_2 perf_3 perf_4 using "${export}\perf_new_measures.tex", /*
*/ keep(HC_distance_t1_s_brut)  cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace  fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N , fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))

esttab perf_5 perf_6 using "${export}\perf_new_measures_interac.tex", /*
*/ keep(HC_buy)  cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace  fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N , fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))


esttab bui_sales1 bui_sales2 bui_sales3 perf_1 perf_2 using "${export}\bui_sales_tot.tex", /*
*/ keep(HC_distance_t1_s_brut)  cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace  fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N, fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))

esttab buy_sales1 buy_sales2 buy_sales3 perf_3 perf_4 using "${export}\buy_sales_tot.tex", /*
*/ keep(HC_distance_t1_s_brut)  cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace  fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N, fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))

esttab interac_sales1 interac_sales2 interac_sales3 perf_5 perf_6 using "${export}\interac_sales_tot.tex", /*
*/ keep(HC_buy)  cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace  fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N, fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))



*******************************************************
* Interaction Perf with organizational capital
*******************************************************

// add OC variables
merge m:1 sirtg year using "${input_stata}\ctrl_var_t1.dta"
tab delta_b _m // we use delta_b==4
drop if _m==2
drop _m


// variables OC
replace admin_t1 = admin_t1
replace mkting_t1= admin_t1 + mkting_t1
replace info_t1= mkting_t1 + info_t1

keep if delta_b==4 // keep only year that merge

* terciles
local varlist rh_t1 mgmt_t1 hr_t1 admin_t1 info_t1 mkting_t1 
foreach x of local varlist {
egen q_`x'= xtile(`x') , nq(3)
drop if q_`x' == .
}

tab q_rh_t1

* dummies
gen dum_hr_t1 = hr_t1 >0 & hr_t1 <. 
tab dum_hr_t1

local varlist rh_t1 mgmt_t1 hr_t1 admin_t1 info_t1 mkting_t1 
foreach x of local varlist {
egen d_`x'= xtile(`x') , nq(2)
drop if d_`x' == .
}

gen HCrh = HC_distance_t1_s_brut*d_rh_t1
label var HCrh "HC Distance$\_{\textit{g,n,t-1}}$ $\times$ 1(High Top layers)$\_{\textit{g,t-1}}$"
label var d_rh_t1 "1(High Top layers)$\_{\textit{g,t-1}}$"

gen HCinfo = HC_distance_t1_s_brut*d_info_t1
label var HCinfo "HC Distance$\_{\textit{g,n,t-1}}$ $\times$ 1(High SG\&A)$\_{\textit{g,t-1}}$"
label var d_info_t1 "1(High SG\&A)$\_{\textit{g,t-1}}$"

gen HCdum_hr_t1 = HC_distance_t1_s_brut*dum_hr_t1
label var HCdum_hr_t1 "HC Distance$\_{\textit{g,n,t-1}}$ $\times$ 1(HR workers)$\_{\textit{g,t-1}}$"
label var dum_hr_t1 "1(HR workers)$\_{\textit{g,t-1}}$"

capture drop sample
reghdfe g_HC_distance_3_7 g_eff_3_7  g_top5_3_7 g_nottop5_3_7 HC_distance_t1  vaj_eff_t1 diversity_t1 /*
*/ size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if  delta_b==4, absorb(orig_dest_year) cluster(code_entry_num apgr_1)
gen sample = e(sample)


****SG&A

eststo clear
// build
reghdfe  log_sales_4 HC_distance_t1_s_brut d_info_t1 HCinfo /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==0& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store info_1
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe  survival_7 HC_distance_t1_s_brut d_info_t1 HCinfo /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==0& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store info_2
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe  g_sales_4_7 HC_distance_t1_s_brut d_info_t1 HCinfo /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==0& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store info_3
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

// buy
reghdfe  log_sales_4 HC_distance_t1_s_brut d_info_t1 HCinfo /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==1& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store info_4
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe  survival_7 HC_distance_t1_s_brut d_info_t1 HCinfo /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==1& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store info_5
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe  g_sales_4_7 HC_distance_t1_s_brut d_info_t1 HCinfo /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==1& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store info_6
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

esttab info_1 info_2 info_3 info_4 info_5 info_6 /*
*/ using "${export}\orgcap_perf.tex", /*
*/ keep(HC_distance_t1_s_brut d_info_t1 HCinfo) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N , fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))


****Layers


eststo clear
// build
reghdfe  log_sales_4 HC_distance_t1_s_brut d_rh_t1 HCrh /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==0& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store rh_1
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe  survival_7 HC_distance_t1_s_brut d_rh_t1 HCrh /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==0& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store rh_2
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe  g_sales_4_7 HC_distance_t1_s_brut d_rh_t1 HCrh /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==0& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store rh_3
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

// build
reghdfe  log_sales_4 HC_distance_t1_s_brut d_rh_t1 HCrh /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==1& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store rh_4
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe  survival_7 HC_distance_t1_s_brut d_rh_t1 HCrh /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==1& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store rh_5
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe  g_sales_4_7 HC_distance_t1_s_brut d_rh_t1 HCrh /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==1& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store rh_6
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

esttab rh_1 rh_2 rh_3 rh_4 rh_5 rh_6 /*
*/ using "${export}\orgcap_perf_rh.tex", /*
*/ keep(HC_distance_t1_s_brut d_rh_t1 HCrh) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N , fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))



****Human resources

eststo clear
// build
reghdfe  log_sales_4 HC_distance_t1_s_brut dum_hr_t1 HCdum_hr_t1 /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==0& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store hr_1
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe  survival_7 HC_distance_t1_s_brut dum_hr_t1 HCdum_hr_t1 /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==0& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store hr_2
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe  g_sales_4_7 HC_distance_t1_s_brut dum_hr_t1 HCdum_hr_t1 /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==0& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store hr_3
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

// build
reghdfe  log_sales_4 HC_distance_t1_s_brut dum_hr_t1 HCdum_hr_t1 /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==1& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store hr_4
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe  survival_7 HC_distance_t1_s_brut dum_hr_t1 HCdum_hr_t1 /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==1& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store hr_5
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe  g_sales_4_7 HC_distance_t1_s_brut dum_hr_t1 HCdum_hr_t1  /*
*/ vaj_eff_t1 diversity_t1  size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 /*
*/ if  delta_b==4&buy==1& sample == 1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store hr_6
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

esttab hr_1 hr_2 hr_3 hr_4 hr_5 hr_6 /*
*/ using "${export}\orgcap_perf_hr.tex", /*
*/ keep(HC_distance_t1_s_brut dum_hr_t1 HCdum_hr_t1) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N , fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))



esttab info_1 rh_1 hr_1 info_4 rh_4 hr_4 /*
*/ using "${export}\orgcap_perf_total.tex", /*
*/ keep(HC_distance_t1_s_brut d_info_t1 HCinfo d_rh_t1 HCrh dum_hr_t1 HCdum_hr_t1) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N , fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))




************************************************************
* Investment in OC
************************************************************
clear 
use "${output_stata}\reg_perf_corrig.dta"

egen orig = group(apgr_1)

*Exports survival
gen survival_5 = s001_5 > 0
gen survival_6 = s001_5 > 0 & s001_6 > 0
gen survival_7 =  s001_7 > 0

foreach x of varlist s001_4 s001_7 {
	su `x',d 
	replace `x' = `r(p95)' if `x' > `r(p95)'
	replace `x' = `r(p5)' if `x' < `r(p5)'
}

gen log_sales_4 = log(s001_4)
gen g_sales_4_7 = (s001_7 - s001_4)/s001_4
gen g_sales_4_5 = (s001_5 - s001_4)/s001_4
gen log_sales_4_7 = log(s001_7/s001_4)

gen log_sales_7 = log(s001_7)

gen g_eff_4_7  = (eff_7 - eff_4)/eff_4
gen g_eff_3_7  = (eff_7 - eff_3)/eff_3
gen g_eff_3_4  = (eff_4 - eff_3)/eff_3
gen g_eff_4_5  = (eff_5 - eff_4)/eff_4
gen d_eff_3_4  = 2*(eff_4 - eff_3)/(eff_3+eff_4)
gen d_eff_3_7  = 2*(eff_7 - eff_3)/(eff_3 + eff_7)

gen g_HC_distance_3_7  = (HC_dist_var_7 - HC_dist_var_3)/HC_dist_var_3
gen g_HC_distance_4_7  = (HC_dist_var_7 - HC_dist_var_4)/HC_dist_var_4
gen g_HC_distance_3_5  = (HC_dist_var_5 - HC_dist_var_3)/HC_dist_var_3
gen g_HC_distance_4_5  = (HC_dist_var_5 - HC_dist_var_4)/HC_dist_var_4

gen d_top5_3_7  = 2*(top5_7 - top5_3)/(eff_3 + eff_7)
gen d_nottop5_3_7  = 2*((eff_7 - top5_7) - (eff_3-top5_3))/(eff_7  + eff_3)

gen g_top5_3_7  = (top5_7 - top5_3)/(eff_3)
gen g_nottop5_3_7  = ((eff_7 - top5_7) - (eff_3-top5_3))/(eff_3)

gen g_top5_3_7b  = (top5_7 - top5_3)/(top5_3)
gen g_nottop5_3_7b  = (not_top5_7 - not_top5_3)/(not_top5_3)

gen d_top5_3_6  = 2*(top5_6 - top5_3)/(eff_3 + eff_6)
gen d_nottop5_3_6  = 2*((eff_6 - top5_6) - (eff_3-top5_3))/(eff_6  + eff_3)
gen d_eff_3_6  = 2*(eff_6 - eff_3)/(eff_3 + eff_6)

set more off
foreach v of varlist g_* d_* {
su `v',d
gen p50 = r(p50)
gen p95 = r(p95)
gen p5 = r(p5)
replace `v' =. if `v'<p50-2*(p95-p5)|`v'>p50+2*(p95-p5)
drop p50 p95 p5
}

label var HC_distance_t1 "HC Distance$\_{\textit{g,n,t-1}}$"

gen buy = entree == "external"
gen HC_buy = HC_distance_t1*buy 
gen q_HC2_buy = q_HC2*buy 

label var HC_distance_t1 "HC Distance$\_{\textit{g,n,t-1}}$"
label var q_HC2 "1(High HC Distance)$\_{\textit{g,n,t-1}}$"
label var buy "1(Buy)$\_{\textit{g,n,t}}$"
label var q_HC2_buy "1(High HC Distance)$\_{\textit{g,n,t-1}}$ $\times$ 1(Buy)$\_{\textit{g,n,t}}$"

*gen HC_buy = HC_distance_t1*buy
label var HC_buy "HC Distance$\_{\textit{g,n,t-1}}$ $\times$ 1(Buy)$\_{\textit{g,n,t}}$"

* calculate delta measure
xtset id delta_b

gen info_delta_3_4 = (info-L.info)/L.info if delta_b==4
gen rh_delta_3_4 =(rh-L.rh)/L.rh if delta_b==4
gen hr_delta_3_4 = hr > 0 & L.hr == 0 
replace hr_delta_3_4 = . if L.hr > 0 | L.hr == .  

foreach x of varlist *delta_3_4 {
	
	su `x',d 
	gen `x'_w = `x' if `x' >= `r(p1)' & `x' <= `r(p99)'
	replace `x'_w = `r(p99)' if `x' > `r(p99)'
	replace `x'_w = `r(p1)' if `x' < `r(p1)'
	su `x'_w,d
}


reghdfe buy HC_distance_t1  vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 if delta_b==4, absorb(orig_dest_year) cluster(code_entry_num apgr_1)
gen sample = e(sample)

label var info_delta_3_4_w "$\Delta$SG\&A$\_{t-1,t}$"
label var rh_delta_3_4_w  "$\Delta$ Top Layer$\_{t-1,t}$"
label var hr_delta_3_4_w  "1(HR$\_{t}>$0$|$HR$\_{t-1}$=0)"

eststo clear
eststo summa: quietly estpost summ ///
info_delta_3_4_w rh_delta_3_4_w hr_delta_3_4_w ///
if sample & delta_b==4 & (buy == 1 | buy == 0), d

esttab summa using "${export}\stat_invorgcap.tex", ///
cells("count(fmt(%15.0fc)) mean(fmt(2)) sd(fmt(2)) p5(fmt(2)) p25(fmt(2)) p50(fmt(2)) p75(fmt(2)) p95(fmt(2))") ///
star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)  /* 
*/ nomtitle noobs nonumber fragment booktabs label mlabels(none) collabels(none) noline replace


eststo clear

****************** BUILD

// SG&A

reghdfe info_delta_3_4_w HC_distance_t1 ///
 vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 ///
if delta_b==4& sample == 1&buy==0, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store ocdelta_1
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

// Layers

reghdfe rh_delta_3_4_w  HC_distance_t1  ///
 vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 ///
if delta_b==4& sample == 1&buy==0, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1) 
estimates store ocdelta_2
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

// HR

reghdfe hr_delta_3_4 HC_distance_t1 ///
 vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 ///
if delta_b==4& sample == 1&buy==0, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store ocdelta_3
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

// SG&A

reghdfe info_delta_3_4_w HC_distance_t1 ///
 vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 ///
if delta_b==4& sample == 1&buy==1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store ocdelta_4
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

*************** BUY

// Layers

reghdfe rh_delta_3_4_w HC_distance_t1 ///
 vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 ///
if delta_b==4& sample == 1&buy==1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1) 
estimates store ocdelta_5
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

// HR

reghdfe hr_delta_3_4 HC_distance_t1 ///
 vaj_eff_t1 diversity_t1 size_t1 immo_eff_t1 sal_eff_t1 tresact_eff_t1 ///
if delta_b==4& sample == 1&buy==1, /*
*/ absorb(code_entry_num apgr_1 year) cluster(code_entry_num apgr_1)
estimates store ocdelta_6
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

reghdfe info_delta_3_4_w HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==4 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)	
estimates store ocdelta_7
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

// Rossi Hansberg

reghdfe rh_delta_3_4_w HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==4 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)	
estimates store ocdelta_8
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

// HR

reghdfe hr_delta_3_4 HC_distance_t1 buy HC_buy c.vaj_eff_t1##i.buy c.diversity_t1##i.buy /*
*/ c.size_t1##i.buy c.immo_eff_t1##i.buy c.sal_eff_t1##i.buy c.tresact_eff_t1##i.buy if delta_b==4 & sample == 1, /*
*/ absorb(i.code_entry_num##i.buy i.orig##i.buy i.year##i.buy) cluster(code_entry_num apgr_1)	
estimates store ocdelta_9
estadd local orig "Yes"
estadd local dest "Yes"
estadd local year "Yes"
estadd local ctrl "Yes"

esttab ocdelta_1 ocdelta_4 ocdelta_2 ocdelta_5 ocdelta_3 ocdelta_6 /*
*/ using "${export}\orgcap_delta.tex", /*
*/ keep(HC_distance_t1_s_brut) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N , fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))

esttab ocdelta_7 ocdelta_8 ocdelta_9  /*
*/ using "${export}\orgcap_delta_interac.tex", /*
*/ keep(HC_buy) cells("b(star fmt(%9.3f))" /*
*/ "se(par fmt(%9.3f))") label /*
*/   alignment(D{.}{.}{-1})  /*     
*/   title(results) star(* 0.10 ** 0.05 *** 0.01) substitute(\_ _)/* 
*/   collabels(none) nodepvars compress nomtitles replace booktabs fragment nonumber /*
*/ stats(ctrl orig dest year r2_a N , fmt(0 0 0 0 3 %15.0fc) /*
*/labels(`"Controls"' `"Sector of Origin FE"' `"Sector of Entry FE"' /*
*/  `"Entry Year FE"' `"Adjusted \(R^{2}\)"' `"Observations"'))

